﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace EMIS
{
    public partial class Helper
    {
        public StudentInfo GetInfo()
        {
            if (sid == null) return null;
            var html = http.Get("http://210.45.240.29/student/asp/xsxxxxx.asp");
            //var html = http.Get("http://127.0.0.1:2011/student/asp/xsxxxxx.asp");
            if (string.IsNullOrWhiteSpace(html)) return null;
            var user = new StudentInfo();
            user.sid = this.sid;

            string reg = @">\s*姓名:\s*(?<name>\w+)\s*<";
            Match match = Regex.Match(html, reg, RegexOptions.IgnoreCase);
            user.Name = match.Groups["name"].Value;

            reg = @">\s*性别:\s*(?<sex>\w+)\s*<";
            match = Regex.Match(html, reg, RegexOptions.IgnoreCase);
            user.Sex = match.Groups["sex"].Value == "男";

            reg = @"班级简称\s*<(?:.|\n)+?<td(?:.|\n)+?>\s*(?<College>\w+)\s*<(?:.|\n)+?<td(?:.|\n)+?>\s*(?<Subject>\w+)\s*<(?:.|\n)+?<td(?:.|\n)+?>\s*(?<Class>(?:\w|-)+)\s*<";
            match = Regex.Match(html, reg, RegexOptions.IgnoreCase);
            user.College = match.Groups["College"].Value;
            user.Subject = match.Groups["Subject"].Value;
            user.Class = match.Groups["Class"].Value;

            reg = @"出生日期\s*<(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?>\s*(?<Nation>\w+?)(?:&nbsp;|\s)*<(?:.|\n)+?<td(?:.|\n)+?>\s*(?<Birthday>(?:\d|-)+)(?:&nbsp;|\s)*<";
            match = Regex.Match(html, reg, RegexOptions.IgnoreCase);
            user.Nation = match.Groups["Nation"].Value;
            user.Birthday = DateTime.Parse(match.Groups["Birthday"].Value);

            reg = @"籍贯\s*<(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?>\s*(?<BirthProvince>\w*?)(?:&nbsp;|\s)*<";
            match = Regex.Match(html, reg, RegexOptions.IgnoreCase);
            user.BirthPlace = match.Groups["BirthProvince"].Value;
            if (string.IsNullOrEmpty(user.BirthPlace))
            {
                reg = @"生源地\s*<(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?>\s*(?<BirthProvince>\w+?)(?:&nbsp;|\s)*<";
                match = Regex.Match(html, reg, RegexOptions.IgnoreCase);
                user.BirthPlace = match.Groups["BirthProvince"].Value;
            }

            reg = @"身份证号\s*<(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?<td(?:.|\n)+?>\s*(?<IDCard>\w+?)(?:&nbsp;|\s)*<";
            match = Regex.Match(html, reg, RegexOptions.IgnoreCase);
            user.IDCard = match.Groups["IDCard"].Value;

            reg = @"性别:(?:.|\n)+?<img\s+src=""\.*(?<img>.+?)""";
            match = Regex.Match(html, reg, RegexOptions.IgnoreCase);
            var imgurl = match.Groups["img"].Value;
            if (!string.IsNullOrWhiteSpace(imgurl))
                user.Photo = http.Download("http://210.45.240.29/student" + imgurl);
                //user.Photo = http.Download("http://127.0.0.1:2011/student" + imgurl);
            return user;
        }
    }
}
